import 'package:chewie/chewie.dart';

import 'package:flutter/material.dart';
import 'package:flutter_syt/datas/message_model.dart';
import 'package:flutter_syt/domain/mine/service/widget/service_messages_goods.dart';
import 'package:flutter_syt/domain/mine/service/widget/service_messages_images.dart';
import 'package:flutter_syt/domain/mine/service/widget/service_messages_text.dart';
import 'package:flutter_syt/domain/mine/service/widget/service_messages_tips.dart';
import 'package:flutter_syt/domain/mine/service/widget/service_messages_video.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/utils/web_socket_utility.dart';

class ServiceMessagesView extends StatefulWidget {
  // final MessageDirection direction;
  final MessageModel messageModel;
  final MsgItemDelegate? delegate;
  final ChewieController? chewieController;

  ServiceMessagesView(
      {required this.messageModel, this.delegate, this.chewieController});
  @override
  ServiceMessagesViewState createState() => ServiceMessagesViewState();
}

class ServiceMessagesViewState extends State<ServiceMessagesView> {
  @override
  Widget build(BuildContext context) {
    return widget.messageModel.type == 10
        ? Container(
            margin: EdgeInsets.only(top: 10), child: _buildMessagsView())
        : widget.messageModel.type == 1
            ? Container(
                margin: EdgeInsets.only(top: 10),
                child: ServiceMessagesTips(
                  model: widget.messageModel,
                ))
            : Container();
  }

  Widget _buildMessagsView() {
    return Row(
      mainAxisAlignment:
          widget.messageModel.direction == MessageDirection.RECEIVE
              ? MainAxisAlignment.start
              : MainAxisAlignment.end,
      children: [
        Visibility(
            visible:
                widget.messageModel.status == SendStatus.SendStatusSendFailed &&
                    widget.messageModel.direction == MessageDirection.SEND,
            child: GestureDetector(
                onTap: ClickUtils.debounce(() {
                  if (widget.delegate != null) {
                    widget.delegate!.didTapFailMessageItem(widget.messageModel);
                  }
                }),
                child: Container(
                  margin: EdgeInsets.only(right: 12),
                  width: 44,
                  height: 44,
                  child: Center(
                    child: Image.asset(
                      'assets/icons/icon_service_fail.png',
                      width: 20,
                      height: 20,
                    ),
                  ),
                ))),
        GestureDetector(
          onTap: ClickUtils.debounce(() {
            if (widget.delegate != null) {
              widget.delegate!.didTapMessageItem(widget.messageModel);
            }
          }),
          child: _buildMessageTypes(),
        ),
      ],
    );
  }

  Widget _buildMessageTypes() {
    if (widget.messageModel.msgType == 1) {
      return ServiceMessagesText(model: widget.messageModel);
    } else if (widget.messageModel.msgType == 2) {
      return ServiceMessagesImages(model: widget.messageModel);
    } else if (widget.messageModel.msgType == 10) {
      return ServiceMessagesGoods(model: widget.messageModel);
    } else if (widget.messageModel.msgType == 4) {
      return ServiceMessagesVideo(
        model: widget.messageModel,
        chewieController: widget.chewieController,
      );
    } else {
      return Container();
    }
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void didUpdateWidget(ServiceMessagesView oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
  }
}

mixin  MsgItemDelegate {
  //点击消息
  void didTapMessageItem(MessageModel message);

  void didTapFailMessageItem(MessageModel message);
}
